package com.viettel.menu;

import com.viettel.rmsmobile.R;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class MenuAdapter extends ArrayAdapter<MenuDTO> {

	Context context;
	int layoutResourceId;
	MenuDTO data[] = null;

	public MenuAdapter(Context context, int resource) {
		super(context, resource);
		this.layoutResourceId = resource;
		this.context = context;
	}

	public MenuAdapter(Context context, int layoutResourceId, MenuDTO[] data) {
        super(context, layoutResourceId, data);
        this.layoutResourceId = layoutResourceId;
        this.context = context;
        this.data = data;
    }
	
	 @Override
	    public View getView(int position, View convertView, ViewGroup parent) {
	        View row = convertView;
	        MenuHolder holder = null;
	       
	        if(row == null)
	        {
	            LayoutInflater inflater = ((Activity)context).getLayoutInflater();
	            row = inflater.inflate(layoutResourceId, parent, false);
	            
	            holder = new MenuHolder();
	            holder.imgIcon = (ImageView)row.findViewById(R.id.imageUrl);
	            holder.txtTitle = (TextView)row.findViewById(R.id.menuName);
	           
	            row.setTag(holder);
	        }
	        else
	        {
	            holder = (MenuHolder)row.getTag();
	        }
	       
	        MenuDTO menu = data[position];
	        holder.txtTitle.setText(menu.getMenuName());
	        holder.imgIcon.setImageResource(menu.getImageUrl());
	       
	        return row;
	    }
	   
	    static class MenuHolder
	    {
	        ImageView imgIcon;
	        TextView txtTitle;
	    }
}
